package com.xingkeduo.business.operation.controller.api;

import com.xingkeduo.business.operation.controller.InitController;
import com.xingkeduo.business.operation.domain.PackageUpdateDomain;
import com.xingkeduo.pojo.dto.hotup.HotUpDTO;
import com.xingkeduo.pojo.table.PackageUpdateTable;
import com.xingkeduo.tool.ResponseUtils;
import com.xingkeduo.util.pojo.exception.SystemException;
import com.xingkeduo.util.tool.ReturnUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static com.xingkeduo.tool.OperaConstants.API_URL_PREFIX;

@RestController
@RequestMapping(API_URL_PREFIX + "/packageUpdate")
public class PackageUpdateController extends InitController {

    @Autowired
    private PackageUpdateDomain packageUpdateDomain;

    @GetMapping(value = "/getPackageUpdateByType")
    public ResponseUtils getPackageUpdateByType(Integer type) {
        returnUtil = new ReturnUtil();
        try {
            HotUpDTO packageUpdateTable = packageUpdateDomain.getPackageUpdateInfoByType(type);
            return ResponseUtils.getSuccess(packageUpdateTable);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return ResponseUtils.error(throwable.getMessage(), SystemException.Type.DEFAULT.toString());
        }
    }


    /**
     * 修改URL
     *
     * @param packageUpdateTable:
     * @return : com.xingkeduo.tool.ResponseUtils
     * @author : gaozifeng
     * @date : 2019/6/26 15:03
     */
    @PostMapping("/update/url")
    public ResponseUtils updateUrl(@RequestBody PackageUpdateTable packageUpdateTable) {
        if (null == packageUpdateTable.getType() || 0L == packageUpdateTable.getType()) {
            return ResponseUtils.error("设备号不能为空", SystemException.Type.DEFAULT.toString());
        }
        try {
            packageUpdateDomain.updateUrl(packageUpdateTable);
            return ResponseUtils.getSuccess();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return ResponseUtils.error(throwable.getMessage(), throwable.getMessage());
        }
    }

    /**
     * 修改URL
     *
     * @return : com.xingkeduo.tool.ResponseUtils
     * @author : gaozifeng
     * @date : 2019/6/26 15:03
     */
    @PostMapping("/get/version")
    public ResponseUtils getVersion() {
        try {
            return ResponseUtils.getSuccess(packageUpdateDomain.selectVersion());
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return ResponseUtils.error(throwable.getMessage(), throwable.getMessage());
        }
    }
}
